use "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\ChinaLead_JCR.dta", clear


xtset leadid2 year

drop if oecd==1



**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 



************************************************
***Figure 1


stcox lnchinadebt_gdp2 $cntl, cluster(ccode)

 set scheme plotplain 

    stcurve, survival at1(lnchinadebt_gdp2=0) at2(lnchinadebt_gdp2=.481) title("") ///
  ytitle("Survival Rate") xtitle("Days in Office") legend(label(1 No China Loan) label(2 Mean China Loan )) ///
  graphregion(color(white)) bgcolor(white) range(1 2500)  name(g1, replace)
  
  
     stcurve,  hazard  at1(lnchinadebt_gdp2=0) at2(lnchinadebt_gdp2=.481) title("") ///
  ytitle("Hazard Rate") xtitle("Days in Office") legend(label(1 No China Loan) label(2 Mean China Loan )) ///
  graphregion(color(white)) bgcolor(white) range(1 2500)  name(g2, replace)
  
  graph combine g1 g2

  
grc1leg g1 g2, legendfrom(g1)
  *graph export "C:\Users\patri\Box\IMFCCP\04 Drafts\pics\me_models12.pdf", as(pdf) replace
  
   * graph export "C:\Users\patri\Box\IMFCCP\04 Drafts\pics\me_models12.png", as(png) replace

************************************************

************************************************
**Table 1


*Model 1 (China Loans)
stcox lnchinadebt_gdp2 $cntl, cluster(ccode)
estimates store c1

*Diagonistic (TVC)
estat phtest, log detail

*Diagonistic (TVC Democracy)
stcox   lnchinadebt_gdp2  $cntl , tvc(v2x_polyarchy) cluster(ccode) texp(ln(_t))

*Model 2 (China Projects)
stcox totalproject2 $cntl , cluster(ccode)
estimates store c2

*Diagonistic (TVC Democracy)
stcox   totalproject2 $cntl , tvc(v2x_polyarchy) cluster(ccode) texp(ln(_t))


*Robustness: Dispersal amount
stcox lntotaldisp2 $cntl , cluster(ccode)



*discrete duration model
probit  fail lnchinadebt_gdp2 $cntl fyrs f2 f3 , cl(ccode)
estimates store c4



***IV**********

**Full model
  cmp (fail=lnchinadebt_gdp2  $cntl fyrs f2 f3) ///
 (lnchinadebt_gdp2= ivch7 prch4 lnres_chn $cntl dgs10 i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store iv3

  cmp (fail=totalproject2  $cntl fyrs f2 f3) ///
 (totalproject2=ivch8 prch5 lnres_chn $cntl dgs10  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store iv4

**Use ivreg to get diagnostic stats 
ivreg2 fail $cntl fyrs f2 f3  ///
(lnchinadebt_gdp2  = ivch7 prch4 lnres_chn i.ccode i.year  ), gmm2s first 

ivregress  gmm  fail $cntl fyrs f2 f3  ///
(lnchinadebt_gdp2  = ivch7 prch4 lnres_chn i.ccode i.year  ), first 

*Post-estimation test (takes awhile to run)
*weakivtest





estout  c1 c2 c4 iv3 iv4 , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 totalproject2 v2x_polyarchy  ) drop(*ccode *year) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 



**********************************
***Table 2

stcox lnchinadebt_gdp2 v2x_execorr lnch_corr $cntl , cluster(ccode)
estimates store i1

gen lnch_corr2=totalproject2*v2x_execorr 
lab var lnch_corr2 "Projects * Corruption"

stcox totalproject2 v2x_execorr lnch_corr2 $cntl , cluster(ccode)
estimates store i1a

*Robust to project dispersal
stcox c.lntotaldisp2##c.v2x_execorr $cntl , cluster(ccode)




stcox lnchinadebt_gdp2 lnch_gr $cntl , cluster(ccode)
estimates store i2

stcox c.totalproject2##c.growth  $cntl , cluster(ccode)
stcox c.lntotaldisp2##c.growth  $cntl , cluster(ccode)

*Public Goods
stcox lnchinadebt_gdp2 v2dlencmps lnch_pg $cntl , cluster(ccode)
estimates store i3

stcox c.totalproject2##c.v2dlencmps $cntl , cluster(ccode)
stcox c.lntotaldisp2##c.v2dlencmps  $cntl , cluster(ccode)

*Military Spending
stcox lnchinadebt_gdp2 lnms_mil_xpnd_gd_zs lnch_mil $cntl , cluster(ccode)
estimates store i4

stcox c.totalproject2##c.lnms_mil_xpnd_gd_zs  $cntl , cluster(ccode)
stcox c.lntotaldisp2##c.lnms_mil_xpnd_gd_zs  $cntl , cluster(ccode)

*Repression
stcox lnchinadebt_gdp2 theta_mean2 lnch_repr $cntl , cluster(ccode)
estimates store i5

stcox c.totalproject2##c.theta_mean2  $cntl , cluster(ccode)
stcox c.lntotaldisp2##c.theta_mean2  $cntl , cluster(ccode)

lab var v2dlencmps "Public Goods"


estout  i1 i1a i2 i3 i4 i5 , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 totalproject2 v2x_execorr lnch_corr lnch_corr2 growth lnch_gr   v2dlencmps lnch_pg  lnms_mil_xpnd_gd_zs lnch_mil    theta_mean2 lnch_repr v2x_polyarchy)  ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 


***********************************************

***Table 3 - offshore accounts


**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 


xtset leadid2 year

reg F.taxpertotal  lnchinadebt_gdp2 v2x_execorr   $cntl  i.ccode
estimates store o0


reg F.taxpertotal    lnchinadebt_gdp2 v2x_execorr lnch_corr   $cntl  i.ccode
estimates store o1

reg F.d.taxpertotal taxpertotal   lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl  i.ccode 
estimates store o2

reg F.Anderspertotal   lnchinadebt_gdp2 v2x_execorr lnch_corr $cntl  i.ccode
estimates store o3

reg F.d.Anderspertotal  Anderspertotal lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl  i.ccode
estimates store o4


lab var lnch_corr "China Debt * Corruption"

estout o0 o1 o2 o3 o4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( r2 N, fmt(%9.2f %9.0f) labels("R2" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 v2x_execorr  lnch_corr   ) drop(*ccode) ///
mlabels ("Model 1"  ) ///
title("") 

*****************************************************
**Table 4 Benchmarks




*baseline
stcox lnchinadebt_gdp2 v2x_execorr   $cntl  ,  cluster(ccode)


stcox lnwb  lnchinadebt_gdp2 v2x_execorr   $cntl  ,  cluster(ccode)
estimates store a0

stcox lnwb  lnchinadebt_gdp2 v2x_execorr wbcorr   $cntl  ,  cluster(ccode)
estimates store a0a


stcox dt_oda_odat_gn_zs lnchinadebt_gdp2 v2x_execorr aidcorr   $cntl  ,  cluster(ccode)
estimates store a1

stcox dt_oda_odat_gn_zs lnchinadebt_gdp2 v2x_execorr   $cntl  ,  cluster(ccode)
estimates store a1a


stcox lnchinadebt_gdp2 offcorr offGNI v2x_execorr  $cntl, cluster(ccode)
estimates store a2

stcox lnchinadebt_gdp2 offGNI v2x_execorr  $cntl, cluster(ccode)
estimates store a2a


stcox lnchinadebt_gdp2 privcorr privGNI v2x_execorr  $cntl, cluster(ccode)
estimates store a3

stcox lnchinadebt_gdp2  privGNI v2x_execorr  $cntl, cluster(ccode)
estimates store a3a


stcox lnchinadebt_gdp2 v2x_execorr ratingsept IRcorr2    $cntl  ,  cluster(ccode)
estimates store a4


stcox lnchinadebt_gdp2 v2x_execorr ratingsept    $cntl  ,  cluster(ccode)
estimates store a4a

stcox lnchinadebt_gdp2 v2x_execorr oilcorr    $cntl  ,  cluster(ccode)
estimates store a5


estout a1a a1 a2a a2 a3a a3 a4a a4 a5,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( lnchinadebt_gdp2  v2x_execorr dt_oda_odat_gn_zs aidcorr offGNI offcorr privGNI privcorr  ratingsept IRcorr2   oilcorr ny_gdp_totl_rt_zs v2x_polyarchy  ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("")   






